package core.proto.vis.predicate;

import prefuse.Visualization;
import prefuse.data.Tuple;
import prefuse.data.expression.AbstractPredicate;
import prefuse.data.expression.OrPredicate;
import prefuse.data.tuple.TupleSet;
import prefuse.visual.EdgeItem;
import prefuse.visual.NodeItem;
import prefuse.visual.VisualItem;

public class ExcludeNonSearchResult extends OrPredicate {

	Visualization vis;
	
	public ExcludeNonSearchResult(Visualization vis){
		this.vis = vis;
	}
	
	public boolean getBoolean(Tuple t){
		TupleSet tuples = vis.getGroup( Visualization.SEARCH_ITEMS );
		if (tuples.getTupleCount() > 0){
			if (t instanceof NodeItem){
				return tuples.containsTuple(t);
			}else if (t instanceof EdgeItem){
				return tuples.containsTuple(((EdgeItem) t).getSourceItem()) && tuples.containsTuple(((EdgeItem) t).getTargetItem());
			}
		}
		// Check allows previous filtering's effect to be preserved. 
		if (t instanceof VisualItem)
			return ((VisualItem) t).isVisible();
		return true;
	}
}
